實作的部分我們就先將整理並分類後的資料載入,接著定義模型
from torch import nn
class my_model(nn.Module):
def __init__(self,in=12,mid=6,out=1):
super(model,self).__init__()
self.in = in
self.mid = mid
self.out = out
self.linear = nn.Sequential(nn.Linear(in,mid),
nn.Linear(mid,out),
nn.ReLU(),
nn.Sigmoid())
def forward(self,x):
x = self.linear(x)
return x
然後就可以訓練並畫出模型訓練過程
import matplotlib.pyplot as plt
accuracy = history.history["accuracy"]
epochs = range(1, len(accuracy)+1)
val_acc = history.history["val_accuracy"]
plt.plot(epochs, accuracy, "b-", label="Training Acc")
plt.plot(epochs, val_acc, "g--", label="Validation Acc")
plt.title("Training and Validation Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
loss = history.history["loss"]
epochs = range(1, len(loss)+1)
val_loss = history.history["val_loss"]
plt.plot(epochs, loss, "b-", label="Training Loss")
plt.plot(epochs, val_loss, "g--", label="Validation Loss")
plt.title("Training and Validation Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.show()
顯示訓練和驗證準確度
顯示訓練和驗證損失
從結果可以看到,準確度隨著epoch次數逐漸準確。
若想更準確可以透過調整其他參數或是對於原先資料及的數據做處理。